package listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener {

    public MySessionListener() {
    }

    public void sessionCreated(HttpSessionEvent arg0) {
    	ServletContext context = arg0.getSession().getServletContext();
    	synchronized (context) {
    		int count = (Integer)context.getAttribute("count");
        	arg0.getSession().getServletContext().setAttribute("count", ++count);
		}
    	
    	System.out.println(arg0.getSession().getId()+"...created");
    }

    public void sessionDestroyed(HttpSessionEvent arg0) {
    	ServletContext context = arg0.getSession().getServletContext();
    	synchronized (context) {
    		int count = (Integer)context.getAttribute("count");
        	arg0.getSession().getServletContext().setAttribute("count", --count);
		}
    	System.out.println(arg0.getSession().getId()+"...destroyed");
    }
	
}
